<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<title>系统用户管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
	Ext.QuickTips.init();
    Ext.define('webuser', {
        extend: 'Ext.data.Model',
        fields: [
            'user_id','user_name','user_realname','user_password','city_id','city_name', 'user_phone', 'user_title','user_type'
        ],
        idProperty: 'user_id'
    });
	
    var store = Ext.create('Ext.data.Store', {
        pageSize: 50,
        model: 'webuser',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/admin_user/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'user_id'
            }
        ]
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "用户ID",
                dataIndex: 'user_id',
                hidden: true
            },{
                text:'城市ID',
                dataIndex:'city_id',
                hidden:true
            },{
				text: "用户登录名",
				dataIndex: 'user_name',
				width: 220
			},{
                text:'用户姓名',
                dataIndex:'user_realname',
                width:220
            },{
                text: '用户电话',
                dataIndex: 'user_phone',
                width : 220
            },{
				text: '用户职务',
				dataIndex: 'user_title',
				width : 220
			},{
				text: '用户职务代码',
				dataIndex: 'user_type',
				width : 220,
				hidden : true
			}
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
			afterPageText: "共 {0} 页",
			firstText:"首页",
			lastText:"末页",
			nextText:"下一页",
			prevText:"上一页",
			refreshText:"刷新"
        }),
        tbar: [
			{
			    pressed: true,
			    icon : '../../img/add.png',
			    text: "添  加",
			    disable:false,
			    handler: addHandler
			},{
			    pressed: true,
			    icon : '../../img/edit.png',
			    text: "修  改",
			    disable:false,
			    handler: updHandler
			},{
				icon : '../../img/delete.png',
				text : '删  除',
				disable:false,
				handler : delHandler
			}]
    });
	store.loadPage(1);
	
	var titleStore = Ext.create('Ext.data.Store', {
		fields: ['user_type', 'title_name'],
		pageSize: 10,
        model: 'title',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/admin_user/user_types',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'user_type'
            }
        ]
	});
	titleStore.loadPage(1);
	
	var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });
	
	var addWin;
    var form;
    form = new Ext.FormPanel({
		id: 'myForm',
		url:'/admin_user/add',
    	method:'POST',
    	frame:true,
    	style:'border-width:0 0 0 0;',
    	bodyBorder:false,
    	bodyPadding: '0 40 0 0',
    	layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
	    items: [
			{
				xtype:'hidden',
				name:'user_id',
				id:'user_id'
			},{
				xtype:'textfield',
				fieldLabel:'用户登录名',
				name:'user_name',
				labelAlign: 'right',
				maxLength:35,
				allowBlank:false,
				tooltip:'请输入登录名',
				id:'user_name',
                width: 100
			},{
                xtype:'textfield',
                fieldLabel:'用户姓名',
                name:'user_realname',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入用户姓名',
                id:'user_realname',
                width: 100
            },{
				xtype:'textfield',
				fieldLabel: '用户电话',
				name: 'user_phone',
				labelAlign: 'right',
				maxLength: 20,
				allowBlank:false,
				tooltip:'请输入用户电话',
				id: 'user_phone',
				width: 100
			},{
				xtype: 'combobox',
				name: 'user_type',
                id: 'user_type',
                fieldLabel: '用户职务',
                labelAlign: 'right',
                store: titleStore,
                editable: false,
                displayField: 'title_name',
                valueField: 'user_type',
                emptyText: '--显示全部--',
                selectOnFocus: true,
                forceSelection: true
			},{
                xtype: 'textfield',
                fieldLabel: '登录密码',
                maxLength:11,
                allowBlank:true,
                name: 'user_password',
                id:'user_password',
				labelAlign: 'right',
				tooltip: '请输入用户登录密码，若不修改密码可不填'
            }
	    ],
	    buttons:[{ 
    		text:'保  存',
    		icon:'../../img/accept.png',
    		handler:function(){
    			if(!form.getForm().isValid()){
    				 Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
    				 return;
                }
                form.form.submit({
                	waitMsg:'正在保存数据...',
                	success:function(form,action){
                		if(action.result.success){
                			Ext.Msg.alert('信息提示',action.result.msg,function(){
                                store.reload();
                                Ext.getCmp('myForm').getForm().reset();
                                addWin.close();
                            });
                		} else {
							Ext.getCmp('myForm').getForm().reset();
                			Ext.Msg.alert('信息提示',action.result.msg);
                		}
                	},
                	failure:function(form,action){
						Ext.getCmp('myForm').getForm().reset();
                		Ext.Msg.alert('信息提示', action.result.msg);
                	}
                });
    		}
    	},{
    		text:'关  闭',
    		icon:'../../img/close.png',
    		handler:function(){
    			this.up('form').getForm().reset();
    			addWin.close();
    		}
    	}]
	});
    
    function updHandler(){
    	var records = grid.getSelectionModel().getSelection();
    	if(records.length > 1){
			Ext.Msg.alert("信息提示","只能选择一条数据进行修改！");
			return;
		} 
    	if(records.length == 0){
    		Ext.Msg.alert("信息提示","请选择需要修改的数据！");
			return;
    	}
        form.form.reset();
    	if(!addWin){
	    	addWin = Ext.create('Ext.Window', {
	    		title: '修改用户信息',
	    		width: 320,
	    		height: 300,
	    		constrainHeader: true,
	    		closeAction:'hide',
	    		layout: 'fit',
	    		items: form
	    	});
    	}
		form.form.loadRecord(records[0]);
		console.log(records[0]);
		form.form.findField('user_type').setValue(records[0].data['user_type']);
    	Ext.getCmp('user_password').setValue('');
        form.getForm().url='/admin_user/upd';
    	addWin.show();
    }
    
    function addHandler(){
    	if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '添加用户信息',
                width: 320,
                height: 300,
                bodyPadding: '0 0 0',
                'z-index': 9999999,
                constrainHeader: true,
                layout: 'fit',
                closeAction:'hide',
                items: form
            });
    	}
		form.getForm().url='/admin_user/add';
		form.getForm().reset();
    	addWin.show();
    }
    
    function delHandler(){
    	var records = grid.getSelectionModel().getSelection();
    	if(records.length == 0){
    		Ext.Msg.alert("信息提示","请选择需要删除的数据！");
			return;
    	}
    	Ext.MessageBox.confirm('信息提示','确认删除吗？',function(val){
    		if(val == 'yes'){
    	    	Ext.Ajax.request({
    	            url: '/admin_user/del/'+records[0].get("user_id"),
    	            method : 'POST',
    	            success: function(response, opts) {
    	                var data = eval("("+response.responseText+")");
    	                Ext.Msg.alert('信息提示',data.msg);
    	                store.reload();
    	            },
    	            failure: function(response, opts) {
    	            	var data = eval("("+response.responseText+")");
    	            	if(!data.success){
    	            		Ext.Msg.alert('信息提示',data.msg);
    	            	} else {
    	            		Ext.Msg.alert("信息提示","系统忙，请稍候再试");
    	            	}
    	            }
    	        });
    		}
    	});
    }
});
</script>
<body>
</body>
</html>